package utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceType;
import com.amazonaws.services.ec2.model.RunInstancesRequest;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.Grant;
import com.amazonaws.services.s3.model.Grantee;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.CreateQueueRequest;


public class AmazonUtils {

	
	public static S3Object readObjectFromS3(AmazonS3 s3, String bucketName, String key) {
		System.out.println("Downloading an object");
		S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
		System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
		return object;
	}
	
	public static String uploadFileToS3(File inputFile, AmazonS3Client s3,
			String bucketName, String key) {
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, inputFile);
//		AccessControlList accessControlList = new AccessControlList();
//		accessControlList.grantPermission(grantee, permission)
//		putObjectRequest.setAccessControlList(accessControlList);
		s3.putObject(putObjectRequest);
		return s3.getResourceUrl(bucketName, key);
	}
	
	public static String createQueue(AmazonSQS sqs, String queueName) {
		CreateQueueRequest r = new CreateQueueRequest(queueName);
		return sqs.createQueue(r).getQueueUrl();
	}
	
	
	public static List<Instance> createInstances(Integer minCount, Integer maxCount, String UserDataScript, AmazonEC2 ec2, String imageId) throws AmazonServiceException{
		try{
			
			RunInstancesRequest request = new RunInstancesRequest();
			request.setImageId(imageId);
			request.setInstanceType(InstanceType.T1Micro.toString());
			request.setMinCount(minCount);
			request.setMaxCount(maxCount);
//			request.setImageId("ami-76f0061f");
			request.setImageId("ami-7609f81f");
//			request.setImageId("ami-bf5eb9d6");
			request.setAdditionalInfo(imageId);
			request.setKeyName("ManagerInstanceKey");
			request.setUserData(UserDataScript);
			return ec2.runInstances(request).getReservation().getInstances();
		}catch (AmazonServiceException ase) {
            System.out.println("Caught Exception: " + ase.getMessage());
            System.out.println("Reponse Status Code: " + ase.getStatusCode());
            System.out.println("Error Code: " + ase.getErrorCode());
            System.out.println("Request ID: " + ase.getRequestId());
        }
		return null;
	}
	
	public static String joinStrings(Collection<String> s, String delimiter){
		StringBuilder builder = new StringBuilder();
		Iterator<String> iter = s.iterator();
		while (iter.hasNext()) {
			builder.append(iter.next());
			if (!iter.hasNext()) {
				break;
			}
			builder.append(delimiter);
		}
		return builder.toString();
	}
	public static StringBuilder readTextFromS3Object(S3Object result) throws IOException {
		InputStreamReader isr = new InputStreamReader(result.getObjectContent());
		BufferedReader in = new BufferedReader(isr);
		StringBuilder htmlSB = new StringBuilder("");
		String inputLine;
		while ((inputLine = in.readLine()) != null)
		    htmlSB.append(inputLine);
		in.close();
		return htmlSB;
	}
}
